package com.onLineCar.netty.controller;

import com.onLineCar.common.core.domain.Result;
import com.onLineCar.common.system.domain.SysUser;
import com.onLineCar.netty.service.NettyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @program: onLineCar-server
 * @description:
 * @author: godGuang
 * @create: 2023-12-14 11:00
 **/
@RestController
public class NettyController {
    @Autowired
    private NettyService nettyService;


    @GetMapping("/getWebSocketServerUrl")
    public Result<String> getWebSocketServerUrl() {
        String webSocketServerUrl = nettyService.getWebSocketServerUrl();
        return Result.success(webSocketServerUrl);
    }

    @GetMapping("/getFriendList/{userName}")
    public Result<List<SysUser>> getFriendList(@PathVariable String userName){
        List<SysUser> friendList = nettyService.getFriendList(userName);
        return Result.success(friendList);
    }

    @PostMapping("/upload")
    public Result fileUpload(@RequestParam("file") MultipartFile file){
        Map<String, Object> data = nettyService.uploadFile(file);
        return Result.success(data,"上传成功");
    }

    @PostMapping("/download")
    public Result download(@RequestParam(value = "fileName", required = true) String fileName,
                                   HttpServletResponse response) throws IOException {
        nettyService.download(fileName,response);
        return Result.success();
    }
}

